package com.example.ttttt.slice;

import com.example.ttttt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.ai.cv.text.TextElement;
import ohos.data.resultset.ResultSet;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.photokit.metadata.AVStorage;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ResponseCache;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button button = findComponentById(ResourceTable.Id_item_select);


        button.setClickedListener(new Component.ClickedListener() {
            @Override public void onClick(Component component) {
                new ToastDialog(getContext()).setText("000").show();
            }
        });
        Image image = findComponentById(ResourceTable.Id_item_image);

//        InputStream inputStream = response.body().byteStream();
//        ImageSource imageSource = ImageSource.create(inputStream, null);
//        PixelMap pixelMap = imageSource.createPixelmap(null);
//

        ImageSource.SourceOptions sourceOptions  = new ImageSource.SourceOptions();



        Text text = findComponentById(ResourceTable.Id_text);
        text.setText(getContext().getCacheDir().getPath());
        byte[] bytes=null;

        ResourceManager resourceManager=getResourceManager();
        try {
            Resource resource = resourceManager.getResource(ResourceTable.Media_image_test);
            bytes = new byte[resource.available()];
            resource.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }

        ImageSource imageSource = ImageSource.create(bytes,null);
        PixelMap pixelMap = imageSource.createPixelmap(null);
        image.setPixelMap(pixelMap);

        DataAbilityHelper dataAbilityHelper = DataAbilityHelper.create(this);
        /**
         * 获取查询的结果集
         *      第一个参数为文件对应的Uri
         *      第二个参数为想要查询的字段，这里因为是查询所有的图片，所以填null即可
         *      第三个参数为查询条件，同样为null
         */
        ResultSet resultSet = null;
        try {
            resultSet = dataAbilityHelper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, null, null);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        //遍历结果集
        while(resultSet != null && resultSet.goToNextRow()){
            //通过指定的下标获取列索引，通过获得的列索引获取到该图片的资源id。
            int mediaId = resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID));
            //将图片id转换为Uri类型
            Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, mediaId + "");
            System.out.println(uri);
        }






        image.setClickedListener(new Component.ClickedListener() {
            @Override public void onClick(Component component) {
                new ToastDialog(getContext()).setText("111").show();
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
